package com.liit.controller.weixin.process;

import org.apache.commons.collections.CollectionUtils;

import cn.liit.weixin.message.model.req.ReqBaseMessage;
import cn.liit.weixin.message.model.resp.RespBaseMessage;
import cn.liit.weixin.message.model.resp.TextMessage;
import cn.liit.weixin.message.process.receive.ReceiveProcessor;

import com.liit.entity.MeetingWxuserFormMap;
import com.liit.entity.MeetingWxuserHisFormMap;
import com.liit.mapper.MeetingWxuserMapper;
import com.liit.spring.SpringUtils;

/**
 * 
 * @author dingweibo
 * @Title: MenuClickProcess.java
 * @Description: 菜单点击事件
 * @date 2016年2月15日
 */
public class MenuClickProcess extends ReceiveProcessor {

	public MenuClickProcess(String msgType, String event) {
		super(msgType, event);
	}

	@Override
	public RespBaseMessage doProcess(ReqBaseMessage reqBaseMessage) {
		RespBaseMessage respBaseMessage = null;
		try {
			String openid = reqBaseMessage.getFromUserName();

			MeetingWxuserMapper mapper = SpringUtils.getBean("meetingWxuserMapper");
			MeetingWxuserFormMap record = mapper.findbyFrist("openid", openid, MeetingWxuserFormMap.class);
			if (record == null) {
				respBaseMessage = new TextMessage("您没有参加任何会议");
			} else {
				// 查询历史记录是否存在
				MeetingWxuserHisFormMap hisFormMap = new MeetingWxuserHisFormMap();
				hisFormMap.put("openid", openid);
				hisFormMap.put("meetingId", record.get("meetingId"));
				if (CollectionUtils.isEmpty(mapper.findByNames(hisFormMap))) {
					hisFormMap.clear();
					hisFormMap.putAll(record);
					mapper.addEntity(hisFormMap);// 添加历史记录
				}
				mapper.deleteByAttribute("id", record.get("id").toString(), MeetingWxuserFormMap.class);// 退出会议
				respBaseMessage = new TextMessage("退出会议成功");
			}
		} catch (Exception e) {
			respBaseMessage = new TextMessage("系统异常,退出会议失败");
			e.printStackTrace();
		}
		return respBaseMessage;
	}
}
